package com.cluster.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cluster.gulimall.common.utils.PageUtils;
import com.cluster.gulimall.common.utils.Query;
import com.cluster.gulimall.product.entity.PmsBrand;
import com.cluster.gulimall.product.entity.PmsCategory;
import com.cluster.gulimall.product.entity.PmsCategoryBrandRelation;
import com.cluster.gulimall.product.mapper.PmsBrandMapper;
import com.cluster.gulimall.product.mapper.PmsCategoryBrandRelationMapper;
import com.cluster.gulimall.product.mapper.PmsCategoryMapper;
import com.cluster.gulimall.product.service.BrandService;
import com.cluster.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class CategoryBrandRelationServiceImpl extends ServiceImpl<PmsCategoryBrandRelationMapper, PmsCategoryBrandRelation> implements CategoryBrandRelationService {

    @Autowired
    private PmsBrandMapper brandMapper;

    @Autowired
    private PmsCategoryMapper categoryMapper;

    @Autowired
    private PmsCategoryBrandRelationMapper relationDao;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PmsCategoryBrandRelation> page = this.page(
                new Query<PmsCategoryBrandRelation>().getPage(params),
                new QueryWrapper<PmsCategoryBrandRelation>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(PmsCategoryBrandRelation categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        //1、查询品牌详细信息
        PmsBrand brandEntity = brandMapper.selectById(brandId);
        //2、查询分类详细信息
        PmsCategory categoryEntity = categoryMapper.selectById(catelogId);

        //将信息保存到categoryBrandRelation中
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        // 保存到数据库中
        this.baseMapper.insert(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        PmsCategoryBrandRelation relationEntity = new PmsCategoryBrandRelation();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);
        this.update(relationEntity, new UpdateWrapper<PmsCategoryBrandRelation>().eq("brand_id", brandId));
    }

    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId, name);
    }

    @Override
    public List<PmsBrand> getBrandsByCatId(Long catId) {

        List<PmsCategoryBrandRelation> catelogId = relationDao.selectList(new QueryWrapper<PmsCategoryBrandRelation>().eq("catelog_id", catId));

        List<PmsBrand> collect = catelogId.stream().map(item -> {
            Long brandId = item.getBrandId();
            //查询品牌的详情
            return brandService.getById(brandId);
        }).collect(Collectors.toList());

        return collect;
    }

}